from rest_framework.views import APIView
from rest_framework.response import Response
from .serializers import SpuSSer,SkuSSer,OptionSer
from .models import SPUSpecification,SPU,SpecificationOption,SKUSpecification
from sku.serializers import SpuSer
# Create your views here.
class GetSpeC(APIView):
    def get(self,request):
        page = int(request.query_params.get('page'))
        size = int(request.query_params.get('size'))
        data = SPUSpecification.objects.all()[((page-1)*size):(page*size)]
        ser = SpuSSer(data,many=True)
        return Response({'data':ser.data,'count':SPUSpecification.objects.all().count()})

class GetSpu(APIView):
    def get(self,request):
        data = SPU.objects.all()
        ser = SpuSer(data,many=True)
        return Response(ser.data)

class OneSpec(APIView):
    def get(self,request,id):
        try:
            data = SPUSpecification.objects.get(id=id)
            ser = SpuSSer(data)
            return Response(ser.data)
        except Exception as e:
            print(e)
            return Response({'msg':'没有该数据','code':204})
    def put(self,request,id):
        name = request.data.get('name')
        spu = request.data.get('spu')
        try:
            SPUSpecification.objects.filter(id=id).update(
                name=name,
                spu_id=spu
            )
            return Response({'msg':'修改成功','code':200})
        except Exception as e:
            print(e)
            return Response({'msg':'修改失败','code':204})
    def delete(self,request,id):
        try:
            SPUSpecification.objects.get(id=id).delete()
            return Response({'msg':'删除成功','code':200})
        except Exception as e:
            print(e)
            return Response({'msg':'没有该数据','code':204})


class AddSpec(APIView):
    def post(self,request):
        name = request.data.get('name')
        spu = request.data.get('spu')
        try:
            SPUSpecification.objects.get(name=name)
            return Response({'msg':'该规格已存在','code':204})
        except Exception as e:
            print(e)
            SPUSpecification.objects.create(
                name=name,
                spu_id=spu
            )
            return Response({'mag':'添加成功','code':200})


class GetOptions(APIView):
    def get(self,request):
        page = int(request.query_params.get('page'))
        size = int(request.query_params.get('size'))
        count = SpecificationOption.objects.all().count()
        data = SpecificationOption.objects.all()[((page-1)*size):(page*size)]
        ser = OptionSer(data,many=True)
        return Response({'data':ser.data,'count':count})

class GetOneOP(APIView):
    def get(self,request,id):
        try:
            data = SpecificationOption.objects.get(id=id)
            ser = OptionSer(data)
            return Response(ser.data)
        except Exception as e:
            print(e)
            return Response({'msg':'没有该数据','code':204})
    def put(self,request,id):
        value = request.data.get('value')
        spec = request.data.get('spec')
        try:
            data = SpecificationOption.objects.get(id=id)
            data.value = value
            data.spec_id = spec
            data.save()
            return Response({'msg': '修改成功', 'code': 200})
        except Exception as e:
            print(e)
            return Response({'msg':'修改失败','code':204})
    def delete(self,request,id):
        try:
            SpecificationOption.objects.get(spec_id=id).delete()
            SPUSpecification.objects.get(id=id).delete()
            return Response({'msg': '删除成功', 'code': 200})
        except Exception as e:
            print(e)
            return Response({'msg':'删除失败','code':204})

class AllSpec(APIView):
    def get(self,request):
        data = SPUSpecification.objects.all()
        ser = SpuSSer(data,many=True)
        return Response(ser.data)

class AddOptions(APIView):
    def post(self,request):
        value = request.data.get('value')
        spec = request.data.get('specs')
        try:
            SpecificationOption.objects.create(
                value=value,
                spec_id=spec
            )
            return Response({'msg':'添加成功','code':200})
        except Exception as e:
            print(e)
            return Response({'msg':'添加成功','code':200})